<template>
  <el-container class="layout-container">
    <el-aside width="220px">
      <div class="sidebar-header">
        <div class="logo-placeholder">LT</div>
        <h2>轨迹记录</h2>
      </div>
      <el-menu
        router
        :default-active="$route.path"
        class="sidebar-menu"
        background-color="#f0f7ff"
        text-color="#2c3e50"
        active-text-color="#1e5eff"
      >
        <el-menu-item index="/">
          <el-icon><HomeFilled /></el-icon>
          <span>首页</span>
        </el-menu-item>
        
        <el-menu-item index="/habits">
          <el-icon><Calendar /></el-icon>
          <span>习惯打卡</span>
        </el-menu-item>
        
        <el-menu-item index="/schedule">
          <el-icon><Clock /></el-icon>
          <span>日程管理</span>
        </el-menu-item>
        
        <el-menu-item index="/todo">
          <el-icon><List /></el-icon>
          <span>待办事项</span>
        </el-menu-item>
        
        <el-menu-item index="/projects">
          <el-icon><Folder /></el-icon>
          <span>项目管理</span>
        </el-menu-item>
        
        <el-menu-item index="/work">
          <el-icon><Document /></el-icon>
          <span>我的工作</span>
        </el-menu-item>
        
        <el-sub-menu index="weekly">
          <template #title>
            <el-icon><Document /></el-icon>
            <span>周报管理</span>
          </template>
          <el-menu-item index="/weekly">
            <el-icon><EditPen /></el-icon>
            <span>周报填写</span>
          </el-menu-item>
          <el-menu-item index="/weekly-stats">
            <el-icon><DataAnalysis /></el-icon>
            <span>周报统计</span>
          </el-menu-item>
          <el-menu-item index="/team-stats">
            <el-icon><User /></el-icon>
            <span>团队周报</span>
          </el-menu-item>
        </el-sub-menu>
        
        <el-menu-item v-if="isAdmin" index="/admin/permissions">
          <el-icon><Setting /></el-icon>
          <span>权限管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header class="main-header">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{getBreadcrumbTitle()}}</el-breadcrumb-item>
        </el-breadcrumb>
        
        <div class="user-actions">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="32" src="/src/assets/avatar.jpg" />
              <span>{{username}}</span>
              <el-icon><CaretBottom /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="settings">系统设置</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import {
  HomeFilled,
  Calendar,
  Clock,
  List,
  Document,
  EditPen,
  DataAnalysis,
  User,
  Setting,
  CaretBottom,
  Folder
} from '@element-plus/icons-vue'

const router = useRouter()
const username = ref('管理员')
const isAdmin = ref(true)

// 获取当前页面的面包屑标题
const getBreadcrumbTitle = () => {
  const route = router.currentRoute.value
  
  if (route.meta && route.meta.title) {
    return route.meta.title
  }
  
  switch (route.name) {
    case 'Home': return '首页'
    case 'Habits': return '习惯打卡'
    case 'Schedule': return '日程管理'
    case 'Todo': return '待办事项'
    case 'Weekly': return '周报填写'
    case 'WeeklyStats': return '周报统计'
    case 'TeamStats': return '团队周报'
    case 'Projects': return '项目管理'
    case 'Work': return '我的工作'
    case 'Permissions': return '权限管理'
    default: return ''
  }
}

// 操作命令处理
const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('isLoggedIn')
    localStorage.removeItem('userRole')
    router.push('/login')
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-aside {
  background-color: #f0f7ff;
  color: #2c3e50;
  height: 100%;
  overflow-x: hidden;
  border-right: 1px solid #e6f1ff;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #1e5eff;
  background-color: #e6f1ff;
  border-bottom: 1px solid #d0e3ff;
}

.logo-placeholder {
  width: 30px;
  height: 30px;
  margin-right: 10px;
  background-color: #1e5eff;
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  font-size: 14px;
}

.sidebar-header h2 {
  font-size: 18px;
  margin: 0;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  padding: 0 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.el-main {
  padding: 0;
  background-color: #f0f2f5;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

:deep(.el-menu) {
  border-right: none;
}

:deep(.el-menu-item) {
  color: #2c3e50;
}

:deep(.el-menu-item.is-active) {
  background-color: #e6f1ff;
  color: #1e5eff;
}

:deep(.el-menu-item:hover) {
  background-color: #e6f1ff;
}

:deep(.el-menu-item .el-icon) {
  color: #2c3e50;
}

:deep(.el-menu-item.is-active .el-icon) {
  color: #1e5eff;
}

:deep(.el-breadcrumb__inner.is-link) {
  color: #1e5eff;
  cursor: pointer;
  font-weight: normal;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #606266;
  font-weight: bold;
}
</style> 